#region Copyright (c) 2013-06, Olaf Kober <amarok.projects@gmail.com>
//================================================================================
//	Permission is hereby granted, free of charge, to any person obtaining a copy
//	of this software and associated documentation files (the "Software"), to deal
//	in the Software without restriction, including without limitation the rights
//	to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//	copies of the Software, and to permit persons to whom the Software is
//	furnished to do so, subject to the following conditions:
//
//	The above copyright notice and this permission notice shall be included in
//	all copies or substantial portions of the Software.
//
//	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//	OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//	THE SOFTWARE.
//================================================================================
#endregion

using System;
using System.Windows;


namespace Amarok.Presentation
{
	/// <summary>
	/// This type represents a Window Description, which is a specific View Description for top-level windows 
	/// and dialogs. A View Description is a description of the combination of view, view model and associated 
	/// controller. A view description is capable to instantiate and connect view and controller and to manage 
	/// the lifetimes of these instances.
	/// </summary>
	public abstract class WindowDescription : ViewDescription
	{
		#region ++ Public Interface ++

		/// <summary>
		/// Gets a reference to the window instance. This property returns null, if Create() was not called before.
		/// </summary>
		public Window Window
		{
			get
			{
				if (base.View == null)
					return null;
				else
					return (Window)base.View.Control;
			}
		}

		#endregion

		#region ~~ Internal Interface ~~

		/// <summary>
		/// Initializes a new instance.
		/// </summary>
		/// 
		/// <param name="windowType">
		/// The type of view in this combination of view, view model and controller.</param>
		/// <param name="controllerType">
		/// The type of controller in this combination of view, view model and controller.</param>
		/// 
		/// <exception cref="ArgumentNullException">
		/// A null reference was passed to a method that did not accept it as a valid argument.</exception>
		/// <exception cref="ArgumentException">
		/// A value was passed to a method that did not accept it as a valid argument, because 
		/// the value representing a type must be a sub-class of specified base type.</exception>
		internal WindowDescription(Type windowType, Type controllerType)
			: base(windowType, controllerType)
		{
			Verify.IsSubclassOf(windowType, typeof(Window), "windowType");
			Verify.IsSubclassOf(controllerType, typeof(Controller), "controllerType");
		}

		#endregion

		#region Implementation

		/// <summary>
		/// This method supports the agent infrastructure and is not intended to be used directly.
		/// </summary>
		protected override void OnCompleted()
		{
			base.OnCompleted();

			if (this.Window == null)
				return;

			ViewHelper.InvokeAsync(
				this.Window,
				() => this.Window.Close()
			);
		}

		#endregion

	}
}
